package com.webemail.sys.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.webemail.sys.mapper.SysContactMapper;
import com.webemail.sys.model.SysContact;
import com.webemail.sys.service.ContactService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author LaiHQ
 */
@Service
public class ContactServiceImpl implements ContactService {
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Resource
	public SysContactMapper sysContactMapper;

	@Override
	public int insert(SysContact temp) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "insert" + " param:" + JSON.toJSONString(temp));
		return sysContactMapper.insert(temp);
	}

	@Override
	public int delete(long id, long userId) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "delete" + " param:" + "id:" + id + "userId:"
				+ userId);
		SysContact temp = new SysContact();
		temp.setId(id);
		temp.setUserId(userId);
		return sysContactMapper.deleteById(temp);
	}

	@Override
	public int update(SysContact temp) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "update" + " param:" + JSON.toJSONString(temp));
		return sysContactMapper.updateById(temp);
	}

	@Override
	public SysContact get(long id, long userId) {
		return sysContactMapper.selectById(id);
	}

	@Override
	public List<SysContact> list(long userId) {
		return sysContactMapper.selectList(Wrappers.lambdaQuery(SysContact.class).eq(SysContact::getUserId,userId));
	}

	@Override
	public long count(long userId) {
		return sysContactMapper.selectCount(Wrappers.lambdaQuery(SysContact.class).eq(SysContact::getUserId,userId));
	}

	@Override
	public List<SysContact> listByPage(long userId, int offset, int limit) {
		Page page = new Page();
		page.setCurrent(offset);
		page.setSize(limit);
		Page pageData = sysContactMapper.selectPage(page, Wrappers.lambdaQuery(SysContact.class)
				.eq(SysContact::getUserId, userId));
		return pageData.getRecords();
	}

}
